﻿using DocumentFormat.OpenXml.Wordprocessing;
using ERP.Common.Results;
using ERP.ERPSystem.HumanResources.Dto;
using ERP.ERPSystem.ProductionSystem.Dto;
using ERP.Team.ProductionSystem;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.ObjectMapping;

namespace ERP.ERPSystem.ProductionSystem
{
    /// <summary>
    /// 生产系统-生产管理-生产领料
    /// </summary>
    public class ProductionpickingService : ApplicationService, IProductionpickingService
    {
        private readonly IRepository<ProductionpickingModel, int> _productionPickingRepository;
      

        public ProductionpickingService(
            IRepository<ProductionpickingModel, int> productionPickingRepository)
        {
            _productionPickingRepository = productionPickingRepository;
        }

        /// <summary>
        /// 添加生产领料单
        /// </summary>
        /// <param name="dto">生产领料DTO</param>
        /// <returns>返回添加结果</returns>
        public async Task<int> AddProductionPickingAsync(ProductionpickingDTO dto)
        {
            var productionPicking = ObjectMapper.Map<ProductionpickingDTO, ProductionpickingModel>(dto);
            var i = await _productionPickingRepository.InsertAsync(productionPicking);
            return i == null ? 0 : 1;
        }
        /// <summary>
        /// 获取所有生产领料单
        /// </summary>
        /// <returns>生产领料单列表</returns>
        public async Task<APIPaging<ProductionpickingModel>> GetAllProductionPickingAsync(string? code, string? IssuanceOrderName, string? IssuanceWarehouse, DateTime? IssuanceDate, string Documentstatus, int page = 1, int limit = 10)
        {
            try
            {
                APIPaging<ProductionpickingModel> pageList = new APIPaging<ProductionpickingModel>();
                var queryable = await _productionPickingRepository.GetQueryableAsync();
                
                // 应用筛选条件
                queryable = queryable.WhereIf(!string.IsNullOrEmpty(code), m => m.IssuanceOrderCode.Contains(code));
                queryable = queryable.WhereIf(!string.IsNullOrEmpty(IssuanceOrderName), m => m.IssuanceOrderName.Contains(IssuanceOrderName));
                queryable = queryable.WhereIf(!string.IsNullOrEmpty(IssuanceWarehouse), m => m.IssuanceWarehouse.Contains(IssuanceWarehouse));
                queryable = queryable.WhereIf(IssuanceDate != null, m => m.IssuanceDate.Date == IssuanceDate.Value.Date);
                queryable = queryable.WhereIf(!string.IsNullOrEmpty(Documentstatus), m => m.Documentstatus == Documentstatus);
                
                // 获取总记录数
                var totalCount = queryable.Count();
                
                // 获取分页数据
                var pagedData = queryable.OrderByDescending(m => m.Id)
                                       .Skip((page - 1) * limit)
                                       .Take(limit)
                                       .ToList();
                
                pageList.PageCount = totalCount; // 总记录数
                pageList.ListDate = pagedData; // 当前页数据
                pageList.PageIndex = page; // 当前页码
                
                return pageList;
            }
            catch (Exception)
            {
                throw;
            }
        }

        /// <summary>
        /// 根据ID获取生产领料单详情
        /// </summary>
        /// <param name="id">领料单ID</param>
        /// <returns>生产领料单详情</returns>
        public async Task<ProductionpickingModel> GetProductionPickingByIdAsync(int id)
        {
            try
            {
                var productionPicking = await _productionPickingRepository.GetAsync(id);
                return productionPicking;
            }
            catch (Exception)
            {
                throw;
            }
        }

        /// <summary>
        /// 执行开票 - 更新单据状态为已完成
        /// </summary>
        /// <param name="id">领料单ID</param>
        /// <returns>更新结果</returns>
        public async Task<bool> ExecuteInvoiceAsync(int id)
        {
            try
            {
                var productionPicking = await _productionPickingRepository.GetAsync(id);
                if (productionPicking == null)
                {
                    return false;
                }

                // 更新单据状态为已完成
                productionPicking.Documentstatus = "已完成";
                await _productionPickingRepository.UpdateAsync(productionPicking);
                
                return true;
            }
            catch (Exception)
            {
                throw;
            }
        }

        /// <summary>
        /// 修改生产领料单
        /// </summary>
        /// <param name="dto">生产领料DTO</param>
        /// <returns>修改结果</returns>
        public async Task<bool> UpdateProductionPickingAsync(ProductionpickingDTO dto)
        {
            try
            {
                var productionPicking = await _productionPickingRepository.GetAsync(dto.Id);
                if (productionPicking == null)
                {
                    return false;
                }

                // 更新字段
                productionPicking.IssuanceOrderCode = dto.IssuanceOrderCode;
                productionPicking.IssuanceOrderName = dto.IssuanceOrderName;
                productionPicking.IssuanceDate = dto.IssuanceDate;
                productionPicking.IssuanceWarehouse = dto.IssuanceWarehouse;
                productionPicking.ProductionWorkOrder = dto.ProductionWorkOrder;
                productionPicking.Documentstatus = dto.Documentstatus;
                productionPicking.CustomerCode = dto.CustomerCode;
                productionPicking.CustomerName = dto.CustomerName;
                productionPicking.ProductionTask = dto.ProductionTask;
                productionPicking.TaskName = dto.TaskName;
                productionPicking.Workstation = dto.Workstation;
                productionPicking.Remark = dto.Remark;

                await _productionPickingRepository.UpdateAsync(productionPicking);
                
                return true;
            }
            catch (Exception)
            {
                throw;
            }
        }

        /// <summary>
        /// 删除生产领料单
        /// </summary>
        /// <param name="id">领料单ID</param>
        /// <returns>删除结果</returns>
        public async Task<bool> DeleteProductionPickingAsync(int id)
        {
            try
            {
                var productionPicking = await _productionPickingRepository.GetAsync(id);
                if (productionPicking == null)
                {
                    return false;
                }

                await _productionPickingRepository.DeleteAsync(productionPicking);
                
                return true;
            }
            catch (Exception)
            {
                throw;
            }
        }
    }
}
